<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" ></meta>
<#include "../common.html">
</head>
<body>
<div  class="easyui-panel" data-options="fit:true,border:false">
	<div class="easyui-layout" data-options="fit:true">
    	<div data-options="region:'center'">  
        	<table id="permissionGrid"></table>
    	</div> 
	</div>
	
	
	
</div>
	<script>
		var permissionGrid;
	$(document).ready(function(){
		permissionGrid = $('#permissionGrid').treegrid({
			url:'/easyui/permission/permTreegrid',
			method:'get',
			// title: 'TreeGrid',
			// iconCls: 'icon-save',
			singleSelect:true,
			idField:'id',  //关键字段来标识树节点，不能重复
			treeField:'name',  //树节点字段，也就是树节点的名称
			fitColumns:true,
			//rownumbers:true,//显示一个行号列
			collapsible:true,//收起表格的内容
			// width: 700,
			// height: 450,
			loadMsg: '数据加载中...',
			animate:true,//在节点展开或折叠的时候是否显示动画效果
			lines:true,//显示treegrid行
			columns:[[
				{field:'id',title:'ID',width:80,align:'center'},
				{field:'name',title:'权限名称',width:100,align:'left'},
				{field:'icon',title:'图标',width:80,align:'center',hidden:true},
				{field:'type',title:'类型',width:80,align:'center',
					//0、目录 1、菜单 2、按钮
					formatter:function(value){
						if(value == 0){
							return '目录';
						}
						if(value == 1){
							return '菜单';
						}
						if(value == 2){
							return '按钮';
						}
					}},
				{field:'sort',title:'排序号',width:80,align:'center'},
				{field:'url',title:'权限url',width:110,align:'center'},
				{field:'permCode',title:'权限编码',width:80,align:'center'},
				{field:'status',title:'状态',width:80,align:'center',
					//0、禁用 1、正常
					formatter:function(value){
						if(value == 0){
							return '禁用';
						}
						if(value == 1){
							return '正常';
						}
					}},
				{field:'gmtCreate',title:'创建时间',width:100,align:'center',formatter:formatDatebox},
				{field:'gmtModified',title:'修改时间',width:100,align:'center',formatter:formatDatebox}
			]],
			toolbar : [{
				iconCls : 'icon-add',
				text : '添加',
				handler : function(){
					showAddPermissionDialog();
				}
			},{
				iconCls : 'icon-edit',
				text : '修改',
				handler : function(){
					var checkUser = permissionGrid.treegrid('getSelections');//选择的用户
					if(checkUser && 1 == checkUser.length){
						showModifyPermissionDialog(checkUser[0]);
					}else{
						parent.$.messager.alert('错误', '请选择一个权限', 'error');
					}
				}
			},{
				iconCls : 'icon-remove',
				text : '删除',
				handler : function(){
					var checkUser = permissionGrid.treegrid('getSelections');//选择的用户

					if(checkUser && checkUser.length == 1){
						deleteCheckUser(checkUser[0]);
					}else{
						parent.$.messager.alert('错误', '请选择一个权限', 'error');
					}
				}
			},{
				iconCls : 'icon-undo',
				text : '导出',
				handler : function(){
					location.href = "/easyui/permission/export";
				}
			}],
			onLoadSuccess:function(data){
				//$('#permissionGrid').treegrid('expandAll');//全部展开树节点
				$('#permissionGrid').treegrid('collapseAll');
			}
		});
	});
	
	function showAddPermissionDialog(){
	   var permissionDialog =  parent.ns.modalDialog({
           title : '添加',
           width : 470,
           height : 430,
           resizable : true,
           url : '/easyui/permission/add',
           buttons : [{
               text : '保存',
               iconCls : 'icon-save',
               handler : function(){
				   permissionDialog.find('iframe').get(0).contentWindow.submitPermission(permissionDialog,permissionGrid,parent.$);
               }
           },{
				text : '关闭',
				iconCls : 'icon-cancel',
				handler : function() {
					permissionDialog.dialog('close');
				}
			}]

       });
	}
	
	function showModifyPermissionDialog(select){
		
		 var permissionDialog =  parent.ns.modalDialog({
	           title : '修改',
	           width : 470,
	           height : 430,
	           resizable : true,
	           url : '/easyui/permission/modify?id='+select.id,
	           buttons : [{
	               text : '保存',
	               iconCls : 'icon-save',
	               handler : function(){
					   permissionDialog.find('iframe').get(0).contentWindow.submitPermission(permissionDialog,permissionGrid,parent.$);
	               }
	           },{
					text : '关闭',
					iconCls : 'icon-cancel',
					handler : function() {
						permissionDialog.dialog('close');
					}
				}]

	       });
	}

	function deleteCheckUser(checkUser){
		
		$.messager.confirm('确认','确认删除'+checkUser.name+'?',function(r){
    	    if (r){
				$.ajax({
					url: "/admin/permission/deletePermissionRole",
					data:  {"id": checkUser.id},
					type: "POST",
					dataType: 'json',
					success: function(result){
						if(result.success){
							//permissionGrid.datagrid('clearSelections').datagrid('clearChecked');
							permissionGrid.treegrid('reload');
							$.messager.alert('提示',result.msg,'info');
						}else{
							$.messager.alert('错误',result.msg,'error');
						}
					}
				});
    	    }  
    	}); 
	}
	</script>
	</body>
</html>